$(window).ready(function(){





    //搜索框左边和导航栏的分类
    $.ajax({
        url:"/index/getMenu",
        type:'get',
        async:false,
        success:function(data){
            var da=data.data;
            // 导航栏Collections的下拉导航
            var collectionNav_li="";
            //搜索框左边的分类
            var collectionOpt="";
            for(var d in da){
                collectionNav_li+="<li> <a href=\"collection.html?id="+da[d].id+" class=\"site-nav__link\">"+da[d].name+"</a> </li>";
                collectionOpt+='<option value="'+da[d].id+'">'+da[d].name+'</option>';
            }
            $("#collectionNav").append(collectionNav_li);
            $("#collection-option").append(collectionOpt);
        }

    });



    var request = GetRequest();
    var sourceCode="";
    var merchantId="";
    var categoryId="";
    var produceId="";
    //返回产品详情
    $.ajax({
        url: "/product/getProduce?productId="+request.id,//3ebc853b3b274defb310c8cdbf47caab
        type: 'get',
        async: false,
        success: function (data) {
            sourceCode=data.sourceCode;
            categoryId=data.categoryId;
            merchantId=data.merchantId;
            produceId=data.produceId;
            //图片展示
            //将图片链接处理一下
            console.log(data)
            var imgs= data.produceImg.substr(0, data.produceImg.length-1).split(",");

            $("#ProductPhotoImg").attr("src",imgs[0]);
            $('div.bigImg_child').css({"background-image":"url("+imgs[0]+")"});

           var lis= $("ul#ProductThumbs").find("li.thumb__element")
            var x;
            if(imgs.length<6){
                for(x=0;x<imgs.length;x++ ){
                    $(lis[x]).find("a").attr("href",imgs[x]);
                    $(lis[x]).find("img").attr("src",imgs[x]);
                }
                for(var i=0;i<6;i++){
                    $(lis[x+i]).find("a").attr("href",imgs[i]);
                    $(lis[x+i]).find("img").attr("src",imgs[i]);
                }
        }
            else if(imgs.length>=6){
                for(x=0;x<5;x++ ){
                    $(lis[x]).find("a").attr("href",imgs[x]);
                    $(lis[x]).find("img").attr("src",imgs[x]);
                }
            }

            //信息展示
            $("div.product-info-left")
            //溯源码
            $("input#search").val(data.sourceCode);
            //商品描述
            $("div#desc").append(data.produceDesc);
            //参数
            $(".product-info-left>h1").text(data.produceName);

            var params=data.produceParameter.split("；");    //这里要注意，是英文的还是中文的
            var paramMap={};
            for(var d in params){
                if(params[d]!="") {//切割后，最后一个会有空格
                    params[d] = params[d].trim();

                    var strings = params[d].split("：");               //这里要注意，是英文的还是中文的

                    if (strings[0] == "更新日期") {
                        var date = new Date(strings[0]);
                        strings[0] = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                    }

                    paramMap[strings[0]] = strings[1];
                }
            }
            var produceShow='';
            for(var k in paramMap){
                     produceShow +=`<div class="`+k+`" itemprop="description">
											`+k+`: <b>`+paramMap[k]+`</b>
							  </div>
                                     `;
            }
            $("div#produceAfter").before(produceShow);

            //价格
            $("select.product-single__variants").before('<div class="selector-wrapper">单价:<b style="margin-left: 10px;">'+data.price+'</b><span>'+data.unit+'</span></div>');






        }

    });



    //固定5条
    $.ajax({
        url: "/product/getProduceList?page=0&limit=5&categoryId="+categoryId,
        type: 'get',
        async: false,
        success: function (data) {
            data=data.data;
            if(data.produceId!=produceId){
                for(var d in data){
                    if(data[d].produceImg!=null&&data[d].produceImg!=""&&data[d].produceImg!=undefined)
                        var imgs= data[d].produceImg.substr(0, data[d].produceImg.length-1).split(",");
                    else
                    {var imgs=new Array();imgs[0]="";}

                    $("ul.related-products-items").find("li.re_li_"+d).find("div.grid__image.product-image").find("a").attr("href","/product.html?produceId="+data[d].produceId);
                    $("ul.related-products-items").find("li.re_li_"+d).find("div.grid__image.product-image").find("img").attr("src",imgs[0]);
                    $("ul.related-products-items").find("li.re_li_"+d).find("div.grid__image.product-image").find("img").attr("alt",data[d].produceName);
                    $("ul.related-products-items").find("li.re_li_"+d).find("p.h6.product-title").find("a").attr("href","/product.html?produceId="+data[d].produceId);
                    $("ul.related-products-items").find("li.re_li_"+d).find("p.h6.product-title").find("a").text(data[d].produceBrief);
                    $("ul.related-products-items").find("li.re_li_"+d).find("p.product-price").find("span").text(data[d].price);
                }
            }


        }
    });




    $.ajax({
        url: "/product/getMerchant",
        type: 'get',
        async: false,
        data:{merchantId:merchantId},
        success: function (data) {

            $("li.picture").find("img").attr("src",data.logo);
            $("li.name").find("a").text(data.merchantName);
            $("div.content-bottom").find("img").attr("src",data.qrcode);
            $("button.phone").append("<span>"+data.contactPhone+"</span>");



        }
    });





    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    //事件绑定
    $('#resFindBtn').on('click',()=>{
        // 点击后获取产品的溯源码
        window.open("http://localhost:8084/traceability/trace-result?traceNumber="+sourceCode)
    });







})